//
//  AppDelegate.m
//  HNBaseProject
//
//  Created by Apple on 2021/7/12.
//

#import "AppDelegate.h"
#import "AppDelegate+SDK.h"
#import "DCTabbarController.h"
#import "ViewController.h"
#import "DCNavigationController.h"
#import "YNSLoginVC.h"
#import "AILoginController.h"
#import "DCMyProductionViewController.h"
#import "YNSHomeController.h"
#import <AppsFlyerLib/AppsFlyerLib.h>
#import "CMUUIDManager.h"
@interface AppDelegate ()
@property (nonatomic,strong) DCTabbarController * tabbarVC;

@end

@implementation AppDelegate

+ (AppDelegate *)shareDelegate {
    return (AppDelegate *)[UIApplication sharedApplication].delegate;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    
    // 初始化第三方SDK
    [[AppsFlyerLib shared] setAppsFlyerDevKey:@"PuDYhVh7foSQSRNxo4xPnm"];
    [[AppsFlyerLib shared] setAppleAppID:@"6670417234"];
//    [self initSDKWithOptions:launchOptions];
    
    //注册登录状态监听
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginStateChange:) name:DC_NotificationName_LoginState object:nil];
//    if ([DCObjectManager dc_readUserDataForKey:DC_Token_Key]) {
//        [[NSNotificationCenter defaultCenter] postNotificationName:DC_NotificationName_LoginState object:@YES];
//    }else{
//        [[NSNotificationCenter defaultCenter] postNotificationName:DC_NotificationName_LoginState object:@NO];
//    }
//    NSString * token = [DCObjectManager dc_readUserDataForKey:DC_Token_Key];
//    if (token) {
        self.window.rootViewController = [[DCNavigationController alloc] initWithRootViewController:[[YNSHomeController alloc] init]];
//    } else {
//        self.window.rootViewController = [[DCNavigationController alloc] initWithRootViewController:[[AILoginController alloc] init]];
//    }
    
    [self.window makeKeyAndVisible];
    
//     NSString *lang = [[NSUserDefaults standardUserDefaults] objectForKey:kLanguageKey];
////    static NSString *kChinese = @"zh-Hans";
////    static NSString *kEnglish = @"en";
//    if (lang) {
//        //设置过语言
//        if ([lang isEqualToString:@"zh-Hans"]) {
//            [NSBundle setLanguage:kChinese];
//        } else {
//            [NSBundle setLanguage:kEnglish];
//        }
//    } else {
//        NSLocale *currentLocale = [NSLocale currentLocale];
//        NSString *languageCode = [currentLocale objectForKey:NSLocaleLanguageCode];
//        NSLog(@"当前语言：%@", languageCode);
//        if ([languageCode isEqualToString:@"zh"]) {
//            [[NSUserDefaults standardUserDefaults] setObject:kChinese forKey:kLanguageKey];
//            [NSBundle setLanguage:kChinese];
//        } else {
//            [[NSUserDefaults standardUserDefaults] setObject:kEnglish forKey:kLanguageKey];
            [NSBundle setLanguage:kEnglish];
//        }
//    }
//    
    
    [self login];
    
    return YES;
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [[AppsFlyerLib shared] start];
}

- (void)login {
    NSString * deviceUID = @"";
    //获得UUID存入keyChain中
    NSUUID * UUID = [UIDevice currentDevice].identifierForVendor;
    NSString * uuid = [CMUUIDManager readUUID];
//    NSLog(@"uuid=======%@",uuid);
    if (uuid == nil) {
        [CMUUIDManager deleteUUID];
        [CMUUIDManager saveUUID:UUID.UUIDString];
        deviceUID = UUID.UUIDString;
    } else {
        deviceUID = uuid;
    }
//    UIDevice *device = [UIDevice currentDevice];//创建设备对象
//    deviceUID = [[NSString alloc] initWithString:device.identifierForVendor.UUIDString];
    MJWeakSelf
    NSDictionary *dict = @{@"udid" : deviceUID};
    [[DCHttpClient shareClient] requestWithPath:@"api/login/uDidBind" params:dict httpMethod:DCHttpRequestPost sucess:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary * dict = [responseObject mj_JSONObject];
        if([dict[@"code"] intValue]==1){
            [weakSelf getUserInfoWithId:dict[@"data"][@"udid"]];
        }
        else {

        }
    } failture:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
}

- (void)getUserInfoWithId:(NSString *)uuid {
    MJWeakSelf
    NSLog(@"uuid=====%@",uuid);
    [[DCHttpClient shareClient] requestWithPath:@"api/login/udidLogin" params:@{@"udid":uuid} httpMethod:DCHttpRequestPost sucess:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary * dict = [responseObject mj_JSONObject];
        if([dict[@"code"] intValue]==1){
            AIUserInfoModel *model = [AIUserInfoModel modelWithDictionary:dict[@"data"]];
            [DCObjectManager dc_saveUserData:[NSString stringWithFormat:@"%@",model.token] forKey:DC_Token_Key];
            [weakSelf getUserInfoWithModel:model];
        } else {

        }
    } failture:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
    }];
}

- (void)getUserInfoWithModel:(AIUserInfoModel *)model {

    MJWeakSelf
    [[DCHttpClient shareClient] requestWithPath:@"api/user/center" params:@{} httpMethod:DCHttpRequestGet sucess:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary * dict = [responseObject mj_JSONObject];
        if([dict[@"code"] intValue]==1){
            NSString *user_money = [NSString stringWithFormat:@"%@",dict[@"data"][@"user_money"]];
            model.user_money = user_money;
            [RQCacheTool saveUserInfo:model];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"loginsuccess" object:nil];
        } else {

        }
    } failture:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
    }];
}

- (void)loginStateChange:(NSNotification *)notification
{
    BOOL bol = [notification.object boolValue];
    if (bol) {
        self.tabbarVC = [[DCTabbarController alloc] init];
        UINavigationController * navigationController = [[UINavigationController alloc] initWithRootViewController:self.tabbarVC];
        self.window.rootViewController = navigationController;
        
        [[DCHelpTool shareClient] dc_bindAlies];
        
    }else{
        
//        LoginVC * loginVC = [[LoginVC alloc] init];
//        UINavigationController * navigationController = [[UINavigationController alloc] initWithRootViewController:loginVC];
//        self.window.rootViewController = navigationController;
//        
//        [[DCHelpTool shareClient] dc_deleteAlies];
        
//        DCTestVC * testVC = [[DCTestVC alloc] init];
//        self.window.rootViewController = testVC;
    }
}


//#pragma mark - UISceneSession lifecycle


//- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
//    // Called when a new scene session is being created.
//    // Use this method to select a configuration to create the new scene with.
//    return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
//}
//
//
//- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
//    // Called when the user discards a scene session.
//    // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
//    // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
//}


@end
